package com.tich.order.controller;


import com.tich.constant.ResponseMessageConstant;
import com.tich.context.UserContext;
import com.tich.order.domain.dto.OrderInsertDTO;
import com.tich.order.domain.dto.OrderSimpleSelectDTO;
import com.tich.order.domain.vo.OrderSelectVO;
import com.tich.order.domain.vo.OrderSimpleSelectVO;
import com.tich.order.service.IOrderService;
import com.tich.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    /**
     * 生成订单
     * @param orderInsertDTO
     * @return
     * */
    @PostMapping
    public Result<String> insertOrder(@RequestBody OrderInsertDTO orderInsertDTO){
        log.info("生成订单：{}", orderInsertDTO);
        return Result.success(ResponseMessageConstant.SAVE_SUCCESS, String.valueOf(iOrderService.insertOrder(orderInsertDTO)));
    }

    /**
     * 取消订单
     * @param id
     * @return
     * */
    @PutMapping
    public Result cancelOrder(Long id){
        log.info("取消订单：{}", id);
        iOrderService.cancelOrder(id);
        return Result.success(ResponseMessageConstant.CANCEL_SUCCESS);
    }

    /**
     * 删除订单
     * @param id
     * @return 
     * */
    @DeleteMapping
    public Result deleteOrder(Long id){
        log.info("删除订单：{}", id);
        iOrderService.deleteOrder(id);
        return Result.success(ResponseMessageConstant.DELETE_SUCCESS);
    }

    /**
     * 查询用户的所有订单简略信息
     * @param orderSimpleSelectDTO
     * @return
     * */
    @PostMapping("/simple")
    public Result<OrderSimpleSelectVO> simpleSelectOrders(@RequestBody OrderSimpleSelectDTO orderSimpleSelectDTO){
        log.info("查询用户的所有订单, userId:{}, {}", UserContext.getCurrentId(), orderSimpleSelectDTO);
        return Result.success(ResponseMessageConstant.QUERY_SUCCESS,
                iOrderService.simpleSelectOrders(orderSimpleSelectDTO));
    }

    /**
     * 查询单个订单的详细信息
     * @param id
     * @return
     * */
    @GetMapping
    public Result<OrderSelectVO> selectOrderById(Long id){
        log.info("查询订单的信息信息：{}", id);
        return Result.success(ResponseMessageConstant.QUERY_SUCCESS, iOrderService.selectOrderById(id));
    }

}
